﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using ClassLibrary.Entity;
using ClassLibrary.DAO;
using System.IO;
using ClassLibrary.Utilities;

public partial class en_ucControl_ManagerPhotos : System.Web.UI.UserControl
{
    TblAlbumImagesDAO AlbumDAO = new TblAlbumImagesDAO(Global.CONNECTION_STRING);
    TblImagesDAO ImageDAO = new TblImagesDAO(Global.CONNECTION_STRING);
    TblAccountsDAO userDAO = new TblAccountsDAO(Global.CONNECTION_STRING);
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            if (Session[Global.SESSION_LOGGED_USER] != null)
            {
                txtEmail.Text = ((TblAccounts)Session[Global.SESSION_LOGGED_USER]).EmailAcc;
                //LoadImagebyIDAcc();
            }
            else
                Response.Redirect("~/en/Default.aspx");
        }
    }
    

    //private void LoadImagebyIDAcc()
    //{
    //    int IDAcc = ((TblAccounts)Session[Global.SESSION_LOGGED_USER]).IDAcc;

    //    DataTable dt = AlbumDAO.GetAlbumsByIDAcc(IDAcc);
    //    grdImageByIDAcc.DataSource = dt;
    //    grdImageByIDAcc.DataBind();
    //    string IDAlbum = "";
    //    foreach (DataRow dr in dt.Rows)
    //    {
    //        IDAlbum = dr["IDAlbum"].ToString();
    //    }
    //    if (IDAlbum == "" || IDAlbum == null)
    //    {
    //        Response.Redirect("~/en/Upload.aspx");
    //    }
    //    else
    //    {
    //        DataTable dt1 = ImageDAO.GetImageByIdAlbum(Convert.ToInt32(IDAlbum));
    //        reImageByAlbum.DataSource = dt1;
    //        reImageByAlbum.DataBind();

    //        foreach (DataRow dr1 in dt.Rows)
    //        {
    //            lblAlbumName.Text = dr1["AlbumName"].ToString();
    //        }
    //    }
    //}
    //protected void btnUpdate_Click(object sender, ImageClickEventArgs e)
    //{
    //    int IdAl = Convert.ToInt32(((ImageButton)sender).CommandArgument);
    //    Response.Redirect("~/en/Upload.aspx?IdAl=" + IdAl);
    //}

    //protected void btnConfirm_Click(object sender, ImageClickEventArgs e)
    //{
    //}

    //protected void grdImageByIDAcc_RowDataBound(object sender, GridViewRowEventArgs e)
    //{
    //    if (e.Row.RowType == DataControlRowType.DataRow)
    //    {
    //        foreach (DataControlFieldCell cell in e.Row.Cells)
    //        {
    //            // check all cells in one row
    //            foreach (Control control in cell.Controls)
    //            {
    //                ImageButton button = control as ImageButton;
    //                if (button != null && button.CommandName == "Delete")
    //                {
    //                    // Add delete confirmation
    //                    //button.CommandArgument = ((TblEmail)e.Row.DataItem).IDEmail.ToString();
    //                    button.OnClientClick = "if (!confirm('Do you want delete this Photo?')) return false;";
    //                }
    //                if (button != null && button.CommandName == "Save")
    //                {
    //                    // Add delete confirmation
    //                    //button.CommandArgument = ((TblEmail)e.Row.DataItem).IDEmail.ToString();
    //                    button.OnClientClick = "if (!confirm('Bạn có muốn lưu không?')) return false;";
    //                }
    //            }
    //        }
    //    }
    //}
    protected void btnSave_Click(object sender, EventArgs e)
    {
        EmailService emailSrv = new EmailService();
        bool bFlag = false;
        try
        {
            if (Session[Global.SESSION_LOGGED_USER] == null)
            {
                return;
            }
            TblAccounts user = (TblAccounts)Session[Global.SESSION_LOGGED_USER];

            if (userDAO.GetUser(txtEmail.Text, Encryption.EncryptToString(txtOldPass.Text)) == null)
            {
                lblMessage.Text = "Invalid Password";
                txtOldPass.Text = txtPassword.Text = txtRePassword.Text = "";
                return;
            }
            user.PasswordAcc = Encryption.EncryptToString(txtPassword.Text);
            userDAO.InsUpdAccounts(user, 3);

            //send mail
            emailSrv.SendChangePasswordEmail(user, Server.MapPath(Global.CHANGE_PASSWORD), txtPassword.Text);


            bFlag = true;

        }
        catch (Exception ex)
        {
            Session[Global.SESSION_ERROR] = ex;
            Response.Redirect(Global.ERROR_PAGE);
        }
        if (bFlag)
        {
            lblMessage.Text = "Password changed successfully";
            txtOldPass.Text = txtPassword.Text = txtRePassword.Text = "";
        }
    }
}
